
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Helper;
using IRepository;
using Models;
using System.Linq;
using Models.Entity;
using MySql.Data.MySqlClient;
using Dapper;
using System.Data;
using System.Linq.Expressions;
using Microsoft.Extensions.Options;
using course.Config;

namespace Repository
{
    /// <summary>
    /// 图片处理服务
    /// </summary>
    public class WeekCourseRepository : IWeekCourseRepository
    {

        DapperHelper<CWeekCourseEntity> dbHelper = new DapperHelper<CWeekCourseEntity>();
        DapperHelper<CClassesEntity> dbHelper_classes = new DapperHelper<CClassesEntity>();
        DapperHelper<CTeacherEntity> dbHelper_teacher = new DapperHelper<CTeacherEntity>();
        DapperHelper<CTeacherCourseEntity> dbHelper_teacherCourse = new DapperHelper<CTeacherCourseEntity>();

        DapperHelper<CClassesWeekCourseEntityView> dbHelper_ref = new DapperHelper<CClassesWeekCourseEntityView>();
        // DapperHelper<CClassesCfgTeacherCourseEntityView> dbHelper_cfg = new DapperHelper<CClassesCfgTeacherCourseEntityView>();


        IClassesCfgRepository classesCfg;
        IOptions<AppConfig> appConfig;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="_classesCfg"></param>
        /// <param name="_appConfig"></param>
        public WeekCourseRepository(IClassesCfgRepository _classesCfg, IOptions<AppConfig> _appConfig)
        {
            classesCfg = _classesCfg;
            appConfig = _appConfig;
        }

        /// <summary>
        /// 添加课程
        /// </summary>
        /// <param name="inEnt"></param>
        /// <returns></returns>
        public async Task<ResultObj<int>> AddWeekCourse(DtoAddWeekCourse inEnt)
        {
            ResultObj<int> resultObj = new ResultObj<int>();
            if (inEnt.classIdList == null || inEnt.classIdList.Count == 0)
            {
                resultObj.success = false;
                resultObj.msg = "班级ID有误";
                return resultObj;
            }

            if (inEnt.order_num < 10)
            {
                resultObj.success = false;
                resultObj.msg = "课时有误";
                return resultObj;
            }

            var teacherCourse = await dbHelper_teacherCourse.Single(x => x.teacher_course_id == inEnt.teacher_course_id);
            if (teacherCourse == null || teacherCourse.teacher_id == 0)
            {
                resultObj.success = false;
                resultObj.msg = "程课ID有误";
                return resultObj;
            }
            int count = await dbHelper_ref.Count(x => x.order_num == inEnt.order_num && x.teacher_id == teacherCourse.teacher_id);
            if (count > 1)
            {
                resultObj.success = false;
                resultObj.msg = $"该老师在[{inEnt.order_num}]有课";
                return resultObj;
            }

            count = await dbHelper_ref.Count($"c.order_num={inEnt.order_num} and a.classes_id in ({String.Join(",", inEnt.classIdList)})");
            if (count > 0)
            {
                resultObj.success = false;
                resultObj.msg = $"选择的班级在[{inEnt.order_num}]有课";
                return resultObj;
            }

            CWeekCourseEntity saveEnt = new CWeekCourseEntity();
            saveEnt.week_course_id = await SequenceRepository.GetNextID<CWeekCourseEntity>();
            saveEnt.teacher_course_id = inEnt.teacher_course_id;
            saveEnt.num = inEnt.num;
            saveEnt.order_num = inEnt.order_num;
            saveEnt.years = inEnt.years;

            dbHelper.TranscationBegin();
            try
            {
                DapperHelper<CClassesWeekCourseEntity> dbHelper_cwc = new DapperHelper<CClassesWeekCourseEntity>(dbHelper.GetConnection(), dbHelper.GetTransaction());
                int opNum = await dbHelper.Save(new DtoSave<CWeekCourseEntity>
                {
                    data = saveEnt,
                });
                if (opNum < 1)
                {
                    throw new Exception("保存课时失败");
                }
                foreach (var item in inEnt.classIdList)
                {
                    opNum = await dbHelper_cwc.Save(new DtoSave<CClassesWeekCourseEntity>
                    {
                        data = new CClassesWeekCourseEntity
                        {
                            classes_id = item,
                            week_course_id = saveEnt.week_course_id
                        },
                    });
                    if (opNum < 1)
                    {
                        throw new Exception("保存班级课时失败");
                    }
                }

                dbHelper.TranscationCommit();
                resultObj.success = true;
                return resultObj;
            }
            catch (System.Exception error)
            {
                dbHelper.TranscationRollback();
                resultObj.success = false;
                resultObj.msg = $"添加失败{error.Message}";
                return resultObj;
            }


        }

        /// <summary>
        /// 删除课时
        /// </summary>
        /// <param name="inEnt"></param>
        /// <returns></returns>
        public async Task<ResultObj<int>> DeleteWeekCourse(DtoDo<int> inEnt)
        {
            ResultObj<int> reObj = new ResultObj<int>();
            DapperHelper db = new DapperHelper();
            db.TranscationBegin();
            try
            {
                int opNum = await db.Exec($"delete c_classes_week_course from c_classes_week_course   where week_course_id={inEnt.Key}");
                if(opNum<1){
                    throw new Exception("删除班级课时失败");
                }
                opNum = await db.Exec($"delete from c_week_course where week_course_id not in (select week_course_id from c_classes_week_course)");
                if(opNum<1){
                    throw new Exception("删除课时失败");
                }
                db.TranscationCommit();
            }
            catch (System.Exception)
            {
                db.TranscationRollback();
                throw;
            }

            return reObj;
        }
        /// <summary>
        /// 生成课表，返回生成课目数
        /// </summary>
        /// <param name="inObj"></param>
        /// <returns></returns>
        public async Task<ResultObj<int>> MakeWeekCourse(DtoDo<int> inObj)
        {

            ResultObj<int> resultObj = new ResultObj<int>();
            var cfgEnt = await classesCfg.SingleByKey(inObj);
            if (cfgEnt == null)
            {
                resultObj.success = false;
                resultObj.msg = "课表还没有配置";
                return resultObj;
            }
            DapperHelper<CWeekCourseEntity> dbHelper_course = new DapperHelper<CWeekCourseEntity>();



            dbHelper_course.TranscationBegin();

            try
            {
                //1、获取所有当前的课表配置，按类型排序，文化课先排

                DapperHelper<CClassesCfgTeacherCourseEntityView> dbHelper_cfg = new DapperHelper<CClassesCfgTeacherCourseEntityView>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CClassesWeekCourseEntityView> dbHelper_ref = new DapperHelper<CClassesWeekCourseEntityView>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CClassesWeekCourseEntity> dbHelper_ClassesWeekCourse = new DapperHelper<CClassesWeekCourseEntity>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CClassesCfgTeacherEntity> dbHelper_CfgTeacher = new DapperHelper<CClassesCfgTeacherEntity>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CClassesCfgTeacherEntityView> dbHelper_CfgTeacher_view = new DapperHelper<CClassesCfgTeacherEntityView>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CClassesCfgEntity> dbHelper_ClassesCfg = new DapperHelper<CClassesCfgEntity>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());
                DapperHelper<CTeacherCourseEntity> dbHelper_TeacherCourse = new DapperHelper<CTeacherCourseEntity>(dbHelper_course.GetConnection(), dbHelper_course.GetTransaction());

                var nowWeekCourse = (await dbHelper_cfg.FindAll(x => x.is_use == 0)).ToList();
                foreach (var item in nowWeekCourse)
                {
                    if (item.tearch_no_course == null) item.tearch_no_course = "";
                    if (item.course_no_course == null) item.course_no_course = "";
                }
                //所有配置
                var allCfg = await dbHelper_ClassesCfg.FindAll(x => x.classes_id != 0);

                //班级已经使用的扩展课表，用于，排课的时候，课表不足，后期手动调整
                var classExtCourseDic = new Dictionary<int, List<int>>();
                //教师已经使用的扩展课表，用于，排课的时候，课表不足，后期手动调整
                var teacherExtCourseDic = new Dictionary<int, List<int>>();

                var allTeacherCourseList = await dbHelper_TeacherCourse.FindAll(x => x.teacher_course_id != 0);
                //所有班级课表
                var weekCourse = (await dbHelper_ref.FindAll(x => x.status == 1)).ToList();

                //每个班的可用的课时
                var weekCourseDic = new Dictionary<int, List<int>>();
                foreach (var item1 in allCfg)
                {
                    //不排课课时
                    var noCourse = allCfg.Single(x => x.classes_id == item1.classes_id).no_course.Split(',').Select(x => Convert.ToInt32(x)).ToList();
                    var classUse = weekCourse.Where(x => x.classes_id == item1.classes_id).Select(x => x.order_num).ToList();
                    classUse = classUse.Union(noCourse).ToList();
                    weekCourseDic.Add(item1.classes_id, MakeCourseTable(classUse));
                }

                //老师可用的课时
                var teacherCourseDic = new Dictionary<int, List<int>>();

                var teacherList = await (new DapperHelper<CTeacherEntity>().FindAll(x => x.teacher_id != 0));
                foreach (var teacher in teacherList)
                {
                    List<int> noCourse = new List<int>();
                    try
                    {

                        //不排课课时
                        if (teacher?.no_course != null)
                        {
                            int tmpi = 0;
                            noCourse = teacher.no_course.Trim(',').Split(',').Where(x => !string.IsNullOrEmpty(x) && int.TryParse(x, out tmpi)).Select(x => Convert.ToInt32(x)).ToList();
                        }
                    }
                    catch (System.Exception e)
                    {
                        Console.WriteLine(e.Message);
                    }
                    teacherCourseDic.Add(teacher.teacher_id, MakeCourseTable(noCourse));
                }

                //班级配置，按场地分组，
                var placeGroup = nowWeekCourse.OrderByDescending(x => x.together_num).GroupBy(x => x.place_id).ToList();
                foreach (var place in placeGroup)
                {
                    List<IGrouping<int, CClassesCfgTeacherCourseEntityView>> teacherGroup;
                    try
                    {
                        teacherGroup = place.OrderByDescending(x => x.together_num).OrderByDescending(x => x.tearch_no_course.Length).OrderByDescending(x => x.course_no_course.Length).GroupBy(x => x.teacher_course_id).ToList();
                    }
                    catch (System.Exception)
                    {

                        throw;
                    }
                    //2、按老师分组,教不同的班

                    //该场地使用的课时
                    List<int> placeUse = new List<int>();

                    //4、轮循每个老师
                    foreach (var teacherG in teacherGroup)
                    {
                        var teacher_course_id = teacherG.Key;
                        var teacherCourse = allTeacherCourseList.Single(x => x.teacher_course_id == teacher_course_id);
                        var teacherId = teacherCourse.teacher_id;
                        //课程不排时间
                        var courseNocourse = new List<int>();
                        if (!string.IsNullOrEmpty(teacherCourse.no_course))
                        {
                            int tempInt = 0;
                            courseNocourse = teacherCourse.no_course.Split(',').Where(x => int.TryParse(x, out tempInt)).Select(x => Convert.ToInt32(x)).ToList();
                        }

                        //课程类型
                        var tearcherType = teacherG.Min(x => x.type_id);
                        //老师连续接排节数
                        var together_num = teacherG.Max(x => x.together_num);
                        if (together_num < 1) together_num = 1;

                        var teacher_name = teacherG.Max(x => x.teacher_name);
                        Console.WriteLine($"开始排[{teacher_name}]老师的课");

                        //只有可能班级不同，才可能
                        foreach (var teacherCifg in teacherG)
                        {
                            //记录操作数据库的返回值
                            int opNum = 0;

                            //5、轮循每个班安装课时
                            for (int i = 0; i < teacherCifg.week_num; i++)
                            {
                                //合班课ID例表
                                var classIdList = teacherCifg.classesid_arr.Split(',').Select(x => Convert.ToInt32(x)).ToList();

                                IList<int> canUse = teacherCourseDic[teacherId];
                                //排除不排课的课时
                                canUse = canUse.Except(courseNocourse).ToList();
                                //差集，排除场地已经使用的课时
                                canUse = canUse.Except(placeUse).ToList();
                                //第一回排课，整理班级课表，把可用的课腾出来
                                if (i == 0)
                                {

                                }
                                //轮循查找老师的空时间和班集空时间交集
                                foreach (var classId in classIdList)
                                {
                                    if (weekCourseDic.ContainsKey(classId))
                                    {
                                        //交集
                                        canUse = canUse.Intersect(weekCourseDic[classId]).ToList();
                                    }
                                }


                                //如果老师没有空时间，则强行使用班级空时间，后期手工调整
                                #region canUse不能为空

                                if (canUse == null || canUse.Count() == 0)
                                {
                                    foreach (var classId in classIdList)
                                    {
                                        if (!classExtCourseDic.ContainsKey(classId))
                                        {
                                            classExtCourseDic.Add(classId, new List<int>());
                                        }
                                    }
                                    if (!teacherExtCourseDic.ContainsKey(teacherId))
                                    {
                                        teacherExtCourseDic.Add(teacherId, new List<int>());
                                    }

                                    var tmpV = 81;
                                    for (tmpV = 81; tmpV < 85; tmpV++)
                                    {
                                        bool isExists = false;
                                        foreach (var classId in classIdList)
                                        {
                                            if (classExtCourseDic[classId].Contains(tmpV))
                                                isExists = true;
                                        }
                                        if (isExists || teacherExtCourseDic[teacherId].Contains(tmpV))
                                            continue;
                                        break;
                                    }
                                    canUse.Add(tmpV);
                                    //在班级扩展里添加课程
                                    foreach (var classId in classIdList)
                                    {
                                        if (!classExtCourseDic[classId].Contains(tmpV))
                                        {
                                            classExtCourseDic[classId].Add(tmpV);
                                        }
                                    }
                                    //在老师扩展里添加
                                    if (!teacherExtCourseDic.ContainsKey(teacherId))
                                    {
                                        teacherExtCourseDic.Add(teacherId, new List<int> { tmpV });
                                    }
                                    else
                                    {
                                        if (!teacherExtCourseDic[teacherId].Contains(tmpV))
                                        {
                                            teacherExtCourseDic[teacherId].Add(tmpV);
                                        }
                                    }
                                }
                                #endregion

                                int getTime = canUse.Min(x => x); ;

                                //如果总课时小于连续课，则取小
                                if ((teacherCifg.week_num - i) < together_num)
                                {
                                    together_num = teacherCifg.week_num - i;
                                }

                                for (int index = 0; index < together_num; index++)
                                {
                                    #region 保存数据

                                    var saveCourse = new CWeekCourseEntity
                                    {
                                        week_course_id = await SequenceRepository.GetNextID<CWeekCourseEntity>(),
                                        teacher_course_id = teacher_course_id,
                                        order_num = getTime
                                    };
                                    opNum = await dbHelper_course.Save(new DtoSave<CWeekCourseEntity>
                                    {
                                        data = saveCourse
                                    });
                                    if (opNum == 0) throw new Exception("保存课表失败");

                                    foreach (var classId in classIdList)
                                    {
                                        opNum = await dbHelper_ClassesWeekCourse.Save(new DtoSave<CClassesWeekCourseEntity>
                                        {
                                            data = new CClassesWeekCourseEntity
                                            {
                                                week_course_id = saveCourse.week_course_id,
                                                classes_id = classId
                                            }
                                        });
                                        if (opNum == 0) throw new Exception("保存班级课表失败");
                                    }


                                    #endregion

                                    #region 移去相关课时

                                    //从班级课表删除获取的时间
                                    foreach (var classId in classIdList)
                                    {
                                        if (weekCourseDic.ContainsKey(classId))
                                        {
                                            weekCourseDic[classId].Remove(getTime);
                                        }
                                    }
                                    //从老师课表删除获取的时间
                                    teacherCourseDic[teacherId].Remove(getTime);

                                    //如果配置了场地则,添加到该场地使用表里
                                    if (place.Key != 0)
                                        placeUse.Add(getTime);

                                    //从能用的列表删除 
                                    canUse.Remove(getTime);
                                    #endregion

                                    //如果是最后一次循环，则不再添加
                                    if ((teacherCifg.week_num - 1) == i)
                                    {
                                        index = together_num;
                                    }


                                    if (index < (together_num - 1))
                                    {
                                        //如果老师没有空时间，则强行使用班级空时间，后期手工调整
                                        #region canUse不能为空
                                        if (canUse == null || canUse.Count() == 0)
                                        {
                                            foreach (var classId in classIdList)
                                            {
                                                if (!classExtCourseDic.ContainsKey(classId))
                                                {
                                                    classExtCourseDic.Add(classId, new List<int>());
                                                }
                                            }
                                            if (!teacherExtCourseDic.ContainsKey(teacherId))
                                            {
                                                teacherExtCourseDic.Add(teacherId, new List<int>());
                                            }

                                            var tmpV = 81;
                                            for (tmpV = 81; tmpV < 85; tmpV++)
                                            {

                                                bool isExists = false;
                                                foreach (var classId in classIdList)
                                                {
                                                    if (classExtCourseDic[classId].Contains(tmpV))
                                                        isExists = true;
                                                }
                                                if (isExists || teacherExtCourseDic[teacherId].Contains(tmpV))
                                                    continue;
                                                break;
                                            }

                                            canUse.Add(tmpV);
                                            //在班级扩展里添加课程
                                            foreach (var classId in classIdList)
                                            {
                                                if (!classExtCourseDic[classId].Contains(tmpV))
                                                {
                                                    classExtCourseDic[classId].Add(tmpV);
                                                }
                                            }
                                            //在老师扩展里添加
                                            if (!teacherExtCourseDic.ContainsKey(teacherId))
                                            {
                                                teacherExtCourseDic.Add(teacherId, new List<int> { tmpV });
                                            }
                                            else
                                            {
                                                if (!teacherExtCourseDic[teacherId].Contains(tmpV))
                                                {
                                                    teacherExtCourseDic[teacherId].Add(tmpV);
                                                }
                                            }
                                        }
                                        #endregion

                                        //判断下一节课是否有空
                                        if (canUse.IndexOf(getTime + 10) > -1)
                                        {
                                            getTime = getTime + 10;
                                        }
                                        else
                                        {
                                            getTime = canUse.Min(x => x);
                                        }
                                    }

                                }
                                //如果有连续课，则跳过课数,排除已经安排的
                                if (together_num > 1)
                                {
                                    i = i + (together_num - 1);
                                }
                            }
                        }
                    }

                }
                //保存成功后，修改配置状态
                Console.WriteLine("更新所有配置状态");
                var opNum1 = await dbHelper_CfgTeacher.Exec("update c_classes_cfg_teacher set is_use=1");

                dbHelper_course.TranscationCommit();
                resultObj.success = true;
                resultObj.data = nowWeekCourse.Sum(x => x.week_num);
            }
            catch (System.Exception e)
            {
                dbHelper_course.TranscationRollback();
                resultObj.success = false;
                resultObj.msg = e.Message;
            }

            return resultObj;
        }

        /// <summary>
        /// 获取年级课表
        /// </summary>
        /// <param name="inObj">班级ID</param>
        /// <returns></returns>
        public async Task<ResultObj<CClassesWeekCourseEntityView>> GetWeekCourse(DtoWeekCourse inObj)
        {
            ResultObj<CClassesWeekCourseEntityView> resultObj = new ResultObj<CClassesWeekCourseEntityView>();
            DapperHelper<CClassesWeekCourseEntityView> dbHelper_ref = new DapperHelper<CClassesWeekCourseEntityView>();

            var reObjList = await dbHelper_ref.FindAll(x => x.classes_id == inObj.ClassesId && x.years == inObj.Years && x.num == inObj.Num);
            resultObj.dataList = reObjList.ToList();
            return resultObj;
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="inObj">主键 ID</param>
        /// <returns></returns>
        public async Task<ResultObj<int>> Delete(DtoWeekCourse inObj)
        {
            ResultObj<int> reObj = new ResultObj<int>();
            DapperHelper db = new DapperHelper();
            db.TranscationBegin();
            try
            {
                await db.Exec($"delete c_classes_week_course a from c_classes_week_course a, c_week_course b where b.years={inObj.Years} and b.num={inObj.Num} and a.classes_id={inObj.ClassesId}");
                await db.Exec($"delete from c_week_course where week_course_id not in (select week_course_id from c_classes_week_course)");
                await db.Exec($"update c_classes_cfg_teacher set is_use=0 where classes_cfg_id in (select b.classes_cfg_id from c_classes_cfg b where b.years={inObj.Years} and b.num={inObj.Num} and a.classes_id={inObj.ClassesId}");
                db.TranscationCommit();
            }
            catch (System.Exception)
            {
                db.TranscationRollback();
                throw;
            }

            return reObj;
        }

        /// <summary>
        /// 生成课表,排除已经存在的，第一位表示第几节课，第二位表示星期
        /// </summary>
        /// <param name="usedList">已经存在的课表</param>
        /// <returns></returns>
        public List<int> MakeCourseTable(List<int> usedList)
        {
            List<int> reList = new List<int>();
            if (appConfig.Value.Course.DayNum > 7) appConfig.Value.Course.DayNum = 7;

            //先算每天的第一节课
            for (int num = 1; num <= appConfig.Value.Course.ClassesNum; num++)
            {
                for (int day = 1; day <= appConfig.Value.Course.DayNum; day++)
                {
                    var v = num * 10 + day;
                    if (!usedList.Contains(v)) reList.Add(v);
                }
            }
            return reList;
        }

        /// <summary>
        /// 获取班级课表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultObj<Dictionary<string, string>>> CourseTable()
        {
            var reObj = new ResultObj<Dictionary<string, string>>();
            DapperHelper<CClassesWeekCourseEntityView> dbHelper_ref = new DapperHelper<CClassesWeekCourseEntityView>();
            try
            {


                var courseList = await dbHelper_ref.FindAll(x => x.week_course_id != 0);

                foreach (var course in courseList.OrderBy(x => x.classes_name).GroupBy(x => x.classes_id))
                {
                    var row = new Dictionary<string, string>();
                    row.Add("班级", course.Max(x => x.classes_name));
                    foreach (var item in course)
                    {
                        var order_num = $"{item.order_num % 10 }-{item.order_num / 10}";
                        if (!row.ContainsKey(order_num))
                        {
                            // row.Add(order_num, $"{item.teacher_name}:{item.course_name}|{item.week_course_id}");
                            row.Add(order_num, $"{item.teacher_name}:{item.course_name}");
                        }
                    }
                    reObj.dataList.Add(row);
                }
                reObj.success = true;
            }
            catch (System.Exception e)
            {
                reObj.success = false;
                reObj.msg = e.Message;
                LogHelper.WriteDebugLog(this.GetType(), e.Message, e);
            }
            return reObj;
        }


        /// <summary>
        /// 获取老师课表
        /// </summary>
        /// <returns></returns>
        public async Task<ResultObj<Dictionary<string, string>>> TeacherCourseTable()
        {
            var reObj = new ResultObj<Dictionary<string, string>>();
            DapperHelper<CClassesWeekCourseEntityView> dbHelper_ref = new DapperHelper<CClassesWeekCourseEntityView>();
            try
            {
                var courseList = await dbHelper_ref.FindAll(x => x.week_course_id != 0);
                foreach (var course in courseList.OrderBy(x => x.teacher_name).GroupBy(x => x.teacher_id))
                {
                    var row = new Dictionary<string, string>();
                    row.Add("老师", course.Max(x => x.teacher_name));
                    foreach (var item in course)
                    {
                        var order_num = $"{item.order_num % 10 }-{item.order_num / 10}";
                        if (!row.ContainsKey(order_num))
                        {
                            // row.Add(order_num, $"{item.teacher_name}:{item.course_name}|{item.week_course_id}");
                            row.Add(order_num, $"{item.teacher_name}:{item.course_name}");
                        }
                    }
                    reObj.dataList.Add(row);
                }
                reObj.success = true;
            }
            catch (System.Exception e)
            {
                reObj.success = false;
                reObj.msg = e.Message;
                LogHelper.WriteDebugLog(this.GetType(), e.Message, e);
            }
            return reObj;
        }

        /// <summary>
        /// 格式化星期节数
        /// </summary>
        /// <param name="week_num"></param>
        /// <returns></returns>
        public string Formart(int week_num)
        {
            return $"星期{week_num % 10}第{week_num / 10}节";
        }

        /// <summary>
        /// 获取班级课表
        /// </summary>
        /// <param name="inObj"></param>
        /// <returns></returns>
        public async Task<ResultObj<CClassesEntity>> GetClassesCourse(DtoDo<int> inObj)
        {
            var reObj = new ResultObj<CClassesEntity>();
            var ent = await dbHelper_classes.Single(x => x.classes_id == inObj.Key);
            var allCourse = (await dbHelper_ref.FindAll(x => x.classes_id == inObj.Key)).ToList();

            ent.courseDic = new Dictionary<string, CClassesWeekCourseEntityView>();
            for (int num = 1; num <= appConfig.Value.Course.ClassesNum + 2; num++)
            {
                for (int day = 1; day <= appConfig.Value.Course.DayNum + 2; day++)
                {
                    ent.courseDic.Add($"a{num}{day}", new CClassesWeekCourseEntityView());

                }
            }
            foreach (var item in allCourse)
            {
                ent.courseDic["a" + item.order_num] = item;
            }
            reObj.data = ent;
            reObj.success = true;
            return reObj;
        }


        /// <summary>
        /// 获取教师课程
        /// </summary>
        /// <param name="inObj"></param>
        /// <returns></returns>
        public async Task<ResultObj<CTeacherEntity>> GetTeacherCourse(DtoDo<int> inObj)
        {
            var reObj = new ResultObj<CTeacherEntity>();
            var ent = await dbHelper_teacher.Single(x => x.teacher_id == inObj.Key);
            var allCourse = (await dbHelper_ref.FindAll(x => x.teacher_id == inObj.Key)).ToList();

            ent.courseDic = new Dictionary<string, CClassesWeekCourseEntityView>();
            for (int num = 1; num <= appConfig.Value.Course.ClassesNum + 2; num++)
            {
                for (int day = 1; day <= appConfig.Value.Course.DayNum + 2; day++)
                {
                    ent.courseDic.Add($"a{num}{day}", new CClassesWeekCourseEntityView());

                }
            }
            foreach (var item in allCourse)
            {
                ent.courseDic["a" + item.order_num] = item;
            }
            reObj.data = ent;
            reObj.success = true;
            return reObj;
        }
    }
}
